/**-----------------------------------------------------------------------------
-通用开发平台:用于客户端操作控制
-作者：张金军
-所有人：张金军
-版权：版权所有，张金军，2005-2015，保留所有权利
-----------------------------------------------------------------------------*/
/**
 * 页面自身注册控件
 */
window.activeElement = null;
window.dataSetID = "winDataSet";
/**
 * 初始化整个页面
 */
function initDocument()
{
	
	var arg = window.dialogArguments;
	
	/* ShowModelDialog 方式处理 */
	
	if(window.windowType == "0"){
		
		/* ShowModelDialog 方式 */
		if (arg && arg.dataSet)
		{	
			addDataset(arg.dataSet);
			eval("window."+arg.dataSet.getID()+" =  arg.dataSet ;");
			window.dataSet = arg.dataSet ;
		}
		
	}else{
		/* DIV 方式处理 */
		if(window.parent 
		&& window.parent.winModel 
		&& window.parent.winModel.arg){
			arg = window.parent.winModel.arg;
			if (arg && arg.dataSet){
				addDataset(arg.dataSet);
				eval("window."+arg.dataSet.getID()+" =  arg.dataSet ;");
				window.dataSet = arg.dataSet ;
			}		
		}
		
	}
	
	/**外部调用函数*/
	if (Yacon.isfireuserEvent("body_onBeforeInit"))
	{
		Yacon.fireUserEvent("body_onBeforeInit",[]);
	}	
	
	setTimeout("_initDocument()",100);
}

function _initDocument()
{
	
	var arg = window.dialogArguments;
	
	if (arg && arg.top)
	{
		window.pratentTop = arg.top;
	}	
	/**
	 * 注册控件信息
	 */
	if (Yacon.isfireuserEvent("regsvrElement"))
	{
		Yacon.fireUserEvent("regsvrElement",[]);
	}
	

	/**外部调用函数*/
	if (Yacon.isfireuserEvent("body_onload"))
	{
		Yacon.fireUserEvent("body_onload",[]);
	}
	
	/* 当前窗体的url和父窗体的 url是不是相同 */
	if(top.location != self.location){  
		   var a = window.document.getElementsByTagName('iframe');  
		   for (var i=0; i<a.length; i++){  
		       a[i].height = document.body.scrollHeight;
		   }  
	}
	 
	Yacon.addEvents(window,"unload",uninitDocument);
	Yacon.addEvents(window,"keydown",body_onclick);
	Yacon.addEvents(window,"resize",body_onresize);
	
	initElement();
}


function initElement()
{
	if (window.dataSet)
	{
		if (!window.dataSet.isEdit()&&!window.dataSet.isInsert())
		{
			if (document.all("save"))
				document.all("save").disabled =true;
		}
	}
	
}


function body_oncontextmenu()
{
	event.returnValue=false;
}

function body_onresize()
{
	
	if (!_array_resizeElement)
		return;
	
	
	for (var i=0;i<_array_resizeElement.length;i++)
	{
		if (_array_resizeElement[i].Control &&_array_resizeElement[i].Control.onresize )
			//_array_resizeElement[i].Control.setWidth(document.body.offsetWidth);
			_array_resizeElement[i].Control.onresize();
	}
}

function body_onclick()
{
	var element   = event.srcElement;	
}

/**
 * 卸载整个页面
 */
function uninitDocument()
{
	for (var i = 0; i < _array_element.length; i++)
	{
		uninitElement(_array_element[i].Control);	
	}
	

	//if (window.dataSet)
	//	window.dataSet.cancelRecord();
}
/**
 * 卸载单个元素
 */
function uninitElement(element)
{
	if (element && element.free)
	{
		element.free();
	}	
	
}
/**
 * 兼容各种浏览器事件对象的获取
 */
if(window.addEventListener) { FixPrototypeForGecko(); }  

function  FixPrototypeForGecko()
{
    HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);
    window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);
    Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);
    Event.prototype.__defineGetter__("fromElement",  element_prototype_get_fromElement);
    Event.prototype.__defineGetter__("toElement", element_prototype_get_toElement);

}  

function  element_prototype_get_runtimeStyle() { return  this.style; }
function  window_prototype_get_event() { return  SearchEvent(); }
function  event_prototype_get_srcElement() { return  this.target; }  

function element_prototype_get_fromElement() {
    var node;
    if(this.type == "mouseover") node = this.relatedTarget;
    else if (this.type == "mouseout") node = this.target;
    if(!node) return;
    while (node.nodeType != 1)
        node = node.parentNode;
    return node;
}

function  element_prototype_get_toElement() {
        var node;
        if(this.type == "mouseout")  node = this.relatedTarget;
        else if (this.type == "mouseover") node = this.target;
        if(!node) return;
        while (node.nodeType != 1)
           node = node.parentNode;
        return node;
}

function  SearchEvent()
{
    if(document.all) return  window.event;  

    func = SearchEvent.caller;  

    while(func!=null){
        var  arg0=func.arguments[0];  

        if(arg0 instanceof Event) {
            return  arg0;
        }
       func=func.caller;
    }
    return   null;
}
/**
 * 按钮控件获取focus
 * @param butObj
 * @return
 */

function buttonAddFocus(butObj)
{
	if(butObj)
	{
		butObj.tabindex ="0";
		butObj.focus();
	    Yacon.addEvents(butObj,"keydown",button_keyDown);
	}
}
function button_keyDown()
{
	 var key     = event.charCode||event.keyCode;
	 if (key ==13)
	 {
		 event.srcElement.fireEvent("onclick")
	 }
}


function common_on_button_click()
{
	var eventElement;
	if (event.srcElement.tagName.toUpperCase()== "SPAN")
		eventElement = event.srcElement;
	if (event.srcElement.tagName.toUpperCase()== "IMG" ||event.srcElement.tagName.toUpperCase()== "LABEL")
		eventElement = event.srcElement.parentNode;
	
	if (!eventElement)
		return
	
	if (Yacon.isfireuserEvent(eventElement.getAttribute("onViewClick")))
			Yacon.fireUserEvent(eventElement.getAttribute("onViewClick"));
	else
	{
		if(Yacon){
			if (Yacon.isfireuserEvent(eventElement.id+"_onPriclick"))
				Yacon.fireUserEvent(eventElement.id+"_onPriclick");
		}	
	}
}

/* 退出  */
function exit_onPriclick()
{
//	if (window.dataSet)
//		window.dataSet.cancelRecord();
	
	if(window.windowType == "0"){
		
		//混合使用情况
		if(window.parent.winModel){
			window.parent.winModel.exit();
		}else{
			window.close();
		}
		
	}else{
		
		//混合使用情况
		if(window.parent.winModel){
			window.parent.winModel.exit();
		}else{
			window.close();
		}
		
	}
	
}

/* 保存  */
function save_onPriclick()
{
	
	if (!page_beforePost()){
		return;
	}
		
	if (window.dataSet && window.dataSet.post(true)){		
		if(window.windowType == "0"){
			window.close();		
		}else{
			window.parent.winModel.save();
		}
	
	}
				
}

function iFrameHeight(){
	var ifm= document.getElementById("detail_frame");   
	var subWeb = document.frames ? document.frames["detail_frame"].document : ifm.contentDocument;   
	if(ifm != null && subWeb != null) {
	   ifm.height = subWeb.body.scrollHeight;
	   ifm.width = subWeb.body.scrollWidth;
	}
}

/**
 * 采用LODOP打印
 * @return
 */
function PrintLODOP(printDoc)
{
	
	if (navigator.appVersion.indexOf("MSIE")<=0)
	{
		alert("目前只支持IE浏览器！");
		return;
	}
	if (!document.getElementById('LODOP_OB'))
	{
		 document.body.insertAdjacentHTML("afterEnd","<object id=\"LODOP_OB\" classid=\"clsid:2105C259-1E0C-4534-8141-A753534CB4CA\" width=0 height=0> </object>");
		 
	}
	if (!document.getElementById('LODOP_OB'))
	{
		alert("打印控件未安装,安装后请刷新页面或重新进入。");
		window.open("/install_lodop.exe");
	}
	var LODOP= document.getElementById('LODOP_OB');  
	/**
	** 根据纸张打印 120MM,930MM
	*/
	LODOP.SET_PRINT_PAGESIZE(1,1200,930,"");
	/**
	**该地方是采用像素标示，需要根据具体环境调整
	*/
	LODOP.ADD_PRINT_TABLE(10,20,420,300,printDoc);
	LODOP.SET_PRINT_STYLEA(0,"FontSize",12);
	LODOP.SET_SHOW_MODE("HIDE_PAPER_BOARD",1);
	//打印浏览
	LODOP.PREVIEW();
	//直接打印
	//LODOP.PRINT();
	//直接选择打印
	//LODOP.PRINTA();
};	

function getLodop(oOBJECT,oEMBED){

	var strHtml1="<br><font color='#FF00FF'>打印控件未安装!点击这里<a href='install_lodop.exe'>执行安装</a>,安装后请刷新页面或重新进入。</font>";
    var strHtml2="<br><font color='#FF00FF'>打印控件需要升级!点击这里<a href='install_lodop.exe'>执行升级</a>,升级后请重新进入。</font>";
    var strHtml3="<br><br><font color='#FF00FF'>注意：<br>1：如曾安装过Lodop旧版附件npActiveXPLugin,请在【工具】->【附加组件】->【扩展】中先卸它;<br>2：如果浏览器表现出停滞不动等异常，建议关闭其“plugin-container”(网上搜关闭方法)功能;</font>";
    var LODOP=oEMBED;		
	try{		     
	     if (navigator.appVersion.indexOf("MSIE")>=0) LODOP=oOBJECT;
	
	     if ((LODOP==null)||(typeof(LODOP.VERSION)=="undefined")) {
		 if (navigator.userAgent.indexOf('Firefox')>=0)
		         document.documentElement.innerHTML=strHtml3+document.documentElement.innerHTML;
		 if (navigator.appVersion.indexOf("MSIE")>=0) document.write(strHtml1); else
		 document.documentElement.innerHTML=strHtml1+document.documentElement.innerHTML;
		 return LODOP; 
	     } else if (LODOP.VERSION<"6.0.5.6") {
		 if (navigator.appVersion.indexOf("MSIE")>=0) document.write(strHtml2); else
		 document.documentElement.innerHTML=strHtml2+document.documentElement.innerHTML; 
		 return LODOP;
	     }
	       return LODOP; 
	}catch(err){
	     document.documentElement.innerHTML="Error:"+strHtml1+document.documentElement.innerHTML;
	     return LODOP; 
	}
}
/**
 * 上传文件
 * @param fileID
 * @return
 */
function upLoadFile(fileID)
{
	var dialogArg = "dialogwidth:600px; Dialogheight:100px;center:yes;help:no;resizable:no;scroll:no;status:no";
	var arg = new Object();
			
	var  dd    = window.showModalDialog(contextPath+"/UI/fileupload/fileupload.jsp?fileID="+fileID,arg, dialogArg);
}

/**
 * 重写退出方法
 */
window.exit = function(){
	window.parent.winModel.exit();
}

